/*
 * @Author: fishroom
 * @Date: 2022-10-12 16:38:07
 * @LastEditTime: 2023-01-03 14:28:24
 * @FilePath: \koa\app\server\SysMuenServer.ts
 */
import { type } from "os";
import { Op } from "sequelize";
import sys_menu from "../model/sys_menu";
import sys_user from "../model/sys_user";
import sys_role_menu from "../model/sys_role_menu";
import sys_role from "../model/sys_role";
class SysMuenServer {
  async getSysMenuNav(role_id: number) {
    let date = await sys_role_menu.findAll({ where: { role_id } });
    let data = date.map((item) => {
      if (item.menu_id == "-666666") {
        return;
      }
      return sys_menu.findOne({ where: { menu_id: item.menu_id } });
    });
    let arr = await Promise.all(data);
    return arr;
  }
  // 没有一级
  async selectSysMenuNav(term: any = undefined) {
    if (term && term.menu_id) {
      term = {
        where: {
          menu_id: {
            [Op.ne]: 0,
          },
        },
      };
    }
    let data = await sys_menu.findAll(term);
    data = data.map((item: any) => {
      return item.dataValues;
    });
    return data;
  }
  async SysMenuNavInfo(term: any = undefined) {
    if (term && term.menu_id) {
      term = {
        where: {
          menu_id: term.menu_id,
        },
      };
    }
    let data = await sys_menu.findAll(term);
    data = data.map((item: any) => {
      return item.dataValues;
    });
    return data;
  }
  async saveSysMenuNav(menu: object) {
    try {
      await sys_menu.create({ ...menu });
      return { error: undefined, data: "添加成功" };
    } catch (error) {
      return { error, data: undefined };
    }
  }
  async updateSysMenuNav(menu: any = {}) {
    try {
      let menu_id = menu.menu_id;
      delete menu.menu_id;
      await sys_menu.update(
        { ...menu },
        {
          where: {
            menu_id,
          },
        }
      );
      return { error: undefined, data: "修改成功" };
    } catch (error) {
      return { error, data: undefined };
    }
  }
  async deleteSysMenu(menu_id: string) {
    sys_menu.destroy({
      where: {
        menu_id: Number(menu_id),
      },
    });
  }
}
export default new SysMuenServer();
